﻿package com.eve.eplus.common.app;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;

import com.eve.eplus.R;
import com.eve.eplus.common.db.SQLiteAccess;
import com.eve.eplus.common.tools.ComDeviceTools;
import com.eve.eplus.common.tools.ComTimeTools;
import com.eve.eplus.common.tools.ConfigurationTools;
import com.eve.eplus.common.tools.LogTools;
import com.eve.eplus.main.logic.SettingUtility;
import com.eve.eplus.sns.SNSAccountManager;
import com.tencent.weibo.oauthv2.OAuthV2;
import com.weibo.sdk.android.Oauth2AccessToken;

/**
 * 全集Application信息
 * 
 * @author Norsie
 * @create 12-11-28
 * 
 */
public class BaseApplication extends Application
{

    /** 应用程序名称 **/
    public static String AppName = null;
    
    /** 应用程序包名 **/
    public static String PackageName = null;

    /** 数据库访问操作 **/
    public static SQLiteAccess mDBAccess = null;

    /** ApplicationContext实例 **/
    public static Context mAppContext = null;

    /** 全局共享对象 **/
    public static Object mShareObj = null;

    /** 网络连接方式 **/
    public static String mNetStyle = null;
    public static int mNetStyleCode = -1;
    
    /** 应用状态 true:非第一次登陆 false：第一次登陆**/
    public static Boolean isFirst = null;
    
    /** 新浪微博 Token信息 **/
    public static Oauth2AccessToken sinaAccessToken;
    
    /** 腾讯微博 Token信息 **/
    public static OAuthV2 tencentAccessToken;
    
    /** SNS绑定帐号数组 仅仅可以用于读取之用（账户管理界面除外）**/    /**由于传递的是地址信息 如果所赋的对象有发生改变会造成数据错误 如果需要值 请通过SNSAccountManager.getAccountList()取得**/
    public static ArrayList<HashMap<String,String>> SNS_ACCOUNT = new ArrayList<HashMap<String,String>>();

    /** 屏幕尺寸 **/
    public static int DisplayWidth = 480;

    public static int DisplayHeight = 800;

    /** 是否进行退出程序操作 **/
    public static boolean isExit =false;

    @Override
    public void onCreate()
    {
        // TODO Auto-generated method stub
        super.onCreate();

        AppName = getResources().getString(R.string.app_name);

        PackageName =getPackageName();
        
        mNetStyle = getReceiver();

        mAppContext = getApplicationContext();
        
        //注册网络状态接收者
        IntentFilter filter = new IntentFilter();  
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);  
        registerReceiver(mNetStateReceiver,filter);

        setFirstLogin();

    }
    

    /**
     * 设置第一次登陆的时间并初始化频道
     */
    private void setFirstLogin()
    {
        // TODO Auto-generated method stub
        String time = ComTimeTools.getTime("yyyy-MM-dd HH:mm:ss");
        if (ConfigurationTools.getInstance(mAppContext).getString(
                Constants.FIRST_LOGIN_TIME, "").equals(""))
        {
            ConfigurationTools.getInstance(mAppContext).putString(
                    Constants.FIRST_LOGIN_TIME, time);
            LogTools.info("Set First Login Time:" + time);
            //初始化设置
            SettingUtility.putDefaultSetting();
            //第一次登陆
            isFirst=true;
        }
        else {
            LogTools.info("First Login Time:"
                    + ConfigurationTools.getInstance(mAppContext).getString(
                            Constants.FIRST_LOGIN_TIME));
            isFirst=false;
        }
    }
    

    /**
     * 获取网络请求管理器.
     * 
     * @return SQLiteAccess
     */
    public static SQLiteAccess getSQLiteAccess()
    {
        if (mDBAccess == null)
        {
            mDBAccess = SQLiteAccess.getInstance(mAppContext);
        }
        return mDBAccess;
    }

    /**
     * 网络处理
     */
    private String getReceiver()
    {
        String mNetStyle = null;
        mNetStyleCode = ComDeviceTools.checkNetWorkType(getApplicationContext());
        switch (mNetStyleCode)
        {
            case Constants.NETWORK_TYPE_NONE:
                mNetStyle = Constants.NETWORK_TYPE_NONE_MSG;
                break;

            case Constants.NETWORK_TYPE_WIFI:
                mNetStyle = Constants.NETWORK_TYPE_WIFI_MSG;
                break;

            case Constants.NETWORK_TYPE_LOW:
                mNetStyle = Constants.NETWORK_TYPE_LOW_MSG;
                break;

            case Constants.NETWORK_TYPE_HIGH:
                mNetStyle = Constants.NETWORK_TYPE_HIGH_MSG;
                break;
            default:
                break;
        }
        return mNetStyle;
    }
    //网络状态接收者
    private BroadcastReceiver mNetStateReceiver = new BroadcastReceiver() {
        
        @Override
        public void onReceive(Context context, Intent intent)
        {
            // TODO Auto-generated method stub
            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                LogTools.info("收到广播：网络状态改变");
                mNetStyle=getReceiver();
            }
        }
    };

}
